package com.windward.vii.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.windward.vii.common.JsonResult;
import com.windward.vii.common.ResultCode;
import com.windward.vii.service.AppFunctionService;
import com.windward.vii.service.AppRecordService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by tiansj on 2017/9/28.
 */
@Controller
public class PlatformController {

    @Resource
    AppRecordService appRecordService;
    @Resource
    AppFunctionService appFunctionService;

    @RequestMapping(value = "v1/api0/app/update/check", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult appUpdateCheck(@RequestParam Integer appType,
                                     @RequestParam Integer versionCode,
                                     HttpServletRequest request) {
        if(appType != 1 && appType != 2) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "appType参数错误(1:Android，2:iOS)");
        }
        if(versionCode <= 0) {
            return new JsonResult(ResultCode.PARAM_ERROR_CODE, "versionCode参数错误");
        }
        return appRecordService.checkUpdate(appType, versionCode);
    }

    @RequestMapping(value = "v1/api1/withdraw/visible", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult withdrawSwitch(HttpServletRequest request,
                                     @RequestHeader(value="User-Agent") String userAgent) {
        return appFunctionService.checkIsShowWithdraw(request.getRequestURI());
    }

    @RequestMapping(value = "v2/api1/withdraw/visible", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult withdrawSwitchV2(HttpServletRequest request,
                                     @RequestHeader(value="User-Agent") String userAgent) {
        boolean isInReview = appRecordService.checkIsInReview(userAgent);
        JSONObject object = new JSONObject();
        object.put("visible", !isInReview);
        JsonResult jsonResult = JsonResult.success();
        jsonResult.setBody(object);
        return jsonResult;
    }

}
